package mytest

import (
	"bytes"
	"fmt"
	"strings"
	"unicode/utf8"
)

func TestString() {
	fmt.Println("testString -------------------")
	var s string = "abcdefg中国"
	fmt.Println(s)

	l := len(s)
	fmt.Println(l)

	fmt.Println(utf8.RuneCountInString(s))

	ss := s[2:5]
	fmt.Println(ss)

	s1 := "abc"
	s2 := "abc"
	fmt.Println(s1 == s2)

	s3 := s1 + s2 + "aaa"
	fmt.Println(s3)

	strArr := []string{s1, s2, s3}
	fmt.Println(strArr)
	fmt.Println(strings.Join(strArr, "|"))

	var buff bytes.Buffer
	buff.WriteString("good")
	buff.WriteString(" ")
	buff.WriteString("bye")
	fmt.Println(buff.String())

	var builder strings.Builder
	builder.WriteString("good")
	builder.WriteString(" ")
	builder.WriteString("bye")
	fmt.Println(builder.String())

	s4 := "abcabcdefabc"
	fmt.Println(strings.Contains(s4, "ab"))

	fmt.Println(strings.Index(s4, "ab"))

	fmt.Println(strings.EqualFold(s4, "ab"))

	fmt.Println(strings.Count(s4, "ab"))

	fmt.Println(strings.ContainsAny(s4, "ac"))

	s5 := "aa bb,cc|dd|ee|ff"
	fmt.Println(strings.Fields(s5))

	fmt.Println(strings.Split(s5, "|"))

	fmt.Println(strings.FieldsFunc(s5, func(r rune) bool {
		if r == ' ' || r == '|' {
			return true
		}
		return false
	}))

	fmt.Println(strings.HasPrefix(s5, "aa "))

	fmt.Println(strings.HasSuffix(s5, "ff"))

	s6 := "Hello,世界! Hello!"
	i := strings.IndexAny(s6, "abc")
	fmt.Println(i) // -1
	i = strings.IndexAny(s6, "dof")
	fmt.Println(i) // 1
	i = strings.IndexAny(s6, "")
	fmt.Println(i) // -1

	s7 := "Hello World!"
	// 创建 Reader
	r := strings.NewReader(s7)
	// 创建读取缓冲区
	b := make([]byte, 5)
	// 读取 r 中指定位置的内容
	r.Seek(6, 0) // 移动索引位置到第 7 个字节
	r.Read(b)    // 开始读取
	fmt.Printf("%q\n", b)
	r.Seek(-5, 1) // 将索引位置移回去
	r.Read(b)     // 继续读取
	fmt.Printf("%q\n", b)
}
